<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="门店id" prop="shopId">
        <el-input
          v-model="queryParams.shopId"
          placeholder="请输入门店id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="门店名称" prop="bokaName">
        <el-input
          v-model="queryParams.bokaName"
          placeholder="请输入门店名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="手机号码" prop="phonenumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.phonenumber"-->
<!--          placeholder="请输入手机号码"-->
<!--          clearable-->
<!--          style="width: 240px"-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

<!--      <el-form-item label="补发师别名" prop="aliasName" label-width="100px">-->
<!--        <el-input-->
<!--          v-model="queryParams.aliasName"-->
<!--          placeholder="请输入补发师别名"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

<!--      <el-form-item label="补发师姓名" prop="nickName" label-width="100px">-->
<!--        <el-input-->
<!--          v-model="queryParams.nickName"-->
<!--          placeholder="请输入补发师姓名"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form-item label="所属军区" prop="theirMilitary">
        <el-select
          v-model="queryParams.theirMilitary"
          placeholder="请选择所属军区"
        >
          <el-option
            v-for="dict in belong_military_region"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            @change="handleQuery"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:hairFixTeacher:add']"
          >新增</el-button
        >
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="Edit"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:hairFixTeacher:edit']"-->
<!--          >修改</el-button-->
<!--        >-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="Delete"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['business:hairFixTeacher:remove']"-->
<!--          >删除</el-button-->
<!--        >-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['business:hairFixTeacher:export']"-->
<!--          >导出</el-button-->
<!--        >-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="refresh"-->
<!--          @click="updateTeacher"-->
<!--          :loading="syncLoading"-->
<!--          v-hasPermi="['business:hairFixTeacher:sync']"-->
<!--        >同步博卡老师数据</el-button-->
<!--        >-->
<!--      </el-col>-->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="hairFixTeacherList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="补发师ID" align="center" prop="id" v-if="true" />
      <el-table-column label="门店ID" align="center" prop="shopId" /> -->
      <el-table-column label="军区" align="center" prop="theirMilitary" />
      <el-table-column label="门店" align="center" prop="bokaName">
        <template #default="scope">
          <template v-for="item in hairFixShopList" :key="item">
            <span v-if="item.id === scope.row.shopId">{{ item.bokaName }}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="补发师名字" align="center" prop="nickName" />
      <el-table-column label="作品内容" align="center" prop="introduceVideo">
        <template #default="scope">
          <template v-if="scope.row.introduceVideo != null">
            <video
              controls
              muted
              :width="200" :height="200"
              type="video/mp4" :src="scope.row.introduceVideo">
            </video>
          </template>
        </template>
      </el-table-column>
<!--      <el-table-column label="补发师别名" align="center" prop="aliasName" />-->
<!--      <el-table-column label="员工编号" align="center" prop="bokaEmpId" />-->
<!--      <el-table-column label="补发师头像" align="center" prop="avatar">-->
<!--        <template #default="scope">-->
<!--          <template v-if="scope.row.avatar != null">-->
<!--            <template v-for="item in scope.row.avatar.split(',')" :key="item">-->
<!--              &lt;!&ndash;              <image-preview :src="item" :width="50" :height="50" />&ndash;&gt;-->
<!--              <el-avatar :src="item"></el-avatar>-->
<!--            </template>-->
<!--          </template>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="手机号" align="center" prop="phonenumber" />-->



      <!-- <el-table-column label="补发师背景" align="center" prop="backImg">
        <template #default="scope">
          <template v-if="scope.row.backImg != null">
            <template v-for="item in scope.row.backImg.split(',')" :key="item">
              <image-preview :src="item" :width="50" :height="50" />
            </template>
          </template>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="微信二维码" align="center" prop="wechatQrCode">
        <template #default="scope">
          <template v-if="scope.row.wechatQrCode != null">
            <template
              v-for="item in scope.row.wechatQrCode.split(',')"
              :key="item"
            >
              <image-preview :src="item" :width="50" :height="50" />
            </template>
          </template>
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        label="从业年限"
        align="center"
        prop="workingSeniority"
      /> -->
      <!-- <el-table-column label="座右铭" align="center" prop="motto" /> -->
<!--      <el-table-column-->
<!--        prop="orderNum"-->
<!--        label="排序"-->
<!--        width="60"-->
<!--      ></el-table-column>-->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
<!--          <div>-->
<!--            <el-button-->
<!--              link-->
<!--              type="primary"-->
<!--              icon="Edit"-->
<!--              @click="toHairTeacherEvaluate(scope.row)"-->
<!--              v-hasPermi="['business:hairFixTeacher:edit']"-->
<!--            >-->
<!--              查看补发师评价列表-->
<!--            </el-button>-->
<!--          </div>-->
          <div>
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['business:hairFixTeacher:edit']"
              >修改</el-button
            >
          </div>
<!--          <div>-->
<!--            <el-button-->
<!--              link-->
<!--              type="primary"-->
<!--              icon="Delete"-->
<!--              @click="handleDelete(scope.row)"-->
<!--              v-hasPermi="['business:hairFixTeacher:remove']"-->
<!--              >删除</el-button-->
<!--            >-->
<!--          </div>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改补发师信息对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" @close="cancel" append-to-body>
      <el-form
        ref="hairFixTeacherRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="所属军区" prop="theirMilitary">
          <el-select v-model="form.theirMilitary"   @change="selectTheirMilitary(form.theirMilitary)" placeholder="请选择所属军区">
            <el-option
              v-for="dict in belong_military_region"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              @change="handleQuery"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="选择门店" prop="shopId">
          <!-- <el-input v-model="form.shopId" placeholder="请输入门店id" /> -->
          <el-select v-model="form.shopId"  @change="selectShop(form.shopId)" filterable>
            <el-option
              v-for="item in hairFixShopList"
              :key="item.id"
              :label="item.shopName"
              :value="item.id"
            /></el-select>
        </el-form-item>
        <el-form-item label="选择老师" prop="id">
          <el-select
            clearable
            v-model="form.id"
            placeholder="选择老师"
          >
            <el-option
              v-for="dict in hairTeacherList"
              :key="dict.id"
              :label="dict.nickName"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作品文件" prop="introduceVideo">
          <videoUpload v-model="form.introduceVideo"></videoUpload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="HairFixTeacher">
import {
  listHairFixTeacher,
  getHairFixTeacher,
  delHairFixTeacher,
  addHairFixTeacher,
  updateHairFixTeacher,
  updateBokaTeacher
} from "@/api/business/hairFixTeacher";
import { listHairFixShop } from "@/api/business/hairFixShop";
import { getRoleUser } from "@/api/system/user";
import {useRoute, useRouter} from "vue-router";

import ImageUploads from "@/components/ImageUploads/index.vue";
import VideoUpload from "@/components/VideoUpload/index.vue";

const router = useRouter();
const route = useRoute()
const { proxy } = getCurrentInstance();
const { sys_teacher_tag, belong_military_region } = proxy.useDict(
  "sys_teacher_tag",
  "belong_military_region"
);
console.log("sys_teacher_tag,", belong_military_region);
const hairFixTeacherList = ref([]); //补发师列表
const hairTeacherList = ref([]); //补发师列表
const hairFixShopList = ref([]); //门店列表
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const teacherUserList = ref([]);
const syncLoading = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    id: route.query != undefined ? route.query?.teacherId : undefined,
    pageNum: 1,
    pageSize: 10,
    shopId: undefined,
    miniUserId: undefined,
    nickName: undefined,
    theirMilitary: undefined,
    avatar: undefined,
    wechatQrCode: undefined,
    workingSeniority: undefined,
    motto: undefined,
    shopName: undefined, //门店名称模糊查询
    aliasName: undefined
  },
  rules: {
    id: [{ required: true, message: "主键id不能为空", trigger: "blur" }],
    shopId: [{ required: true, message: "门店id不能为空", trigger: "blur" }],
    nickName: [{ required: true, message: "昵称不能为空", trigger: "blur" }],
    avatar: [
      { required: true, message: "补发师头像不能为空", trigger: "blur" },
    ],
    phonenumber: [
      {
        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
        message: "请输入正确的手机号码",
        trigger: "blur",
      },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);
function selectTheirMilitary(row){
  getShopList(row)
  getTeacherList(row)
  form.value.shopId = null;
  form.value.id = null;
}

function selectShop(row){
  getTeacherList("",row)
  form.value.id = null;
}
getShopList();
getTeacherList();
/** 查询门店列表 */
function getShopList(theirMilitary) {
  listHairFixShop({
    pageNum: 1,
    pageSize: 999999,
    theirMilitary: theirMilitary}).then((response) => {
    hairFixShopList.value = response.rows;
  });
}
function getTeacherList(theirMilitary,shopId) {
  listHairFixTeacher({
    pageNum: 1,
    pageSize: 999999,
    theirMilitary: theirMilitary,
    shopId:shopId
  }).then((response) => {
    hairTeacherList.value = response.rows;
  });
}
// getRoleUserList()
/** 根据角色key查询用户列表 */
// function getRoleUserList(){
//   getRoleUser({"roleKey": "teacher"}).then(response =>{
//     teacherUserList.value =  response.data
//   })
//
// }

/**搜索门店列表 */
// function searchHairFixShopList(query) {
//   if (query) {
//     loading.value = true;
//     setTimeout(() => {
//       loading.value = false;
//       hairFixShopList.value = hairFixShopList.value.filter((item) => {
//         return item.shopName.toLowerCase().includes(query.toLowerCase());
//       });
//     }, 200);
//   } else {
//     hairFixShopList.value = [];
//   }
// }

function toHairTeacherEvaluate(row) {
  console.log(row);
  // this.$router.push({ name: 'hairTeacherEvaluate', query: { teacherId: row.id } });

  router.push({
    path: "/gallantry/hairTeacherEvaluate",
    query: { teacherId: row.id },
  });
}

/** 查询补发师信息列表 */
function getList() {
  console.log(queryParams.value);
  loading.value = true;
  listHairFixTeacher(queryParams.value).then((response) => {
    hairFixTeacherList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
getList();
// 取消按钮
function cancel() {
  open.value = false;
  reset();
  getShopList();
  getTeacherList();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    shopId: null,
    miniUserId: null,
    nickName: null,
    theirMilitary: null,
    avatar: null,
    wechatQrCode: null,
    workingSeniority: null,
    motto: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
  };
  proxy.resetForm("hairFixTeacherRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加补发师信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true;
  reset();
  const _id = row.id || ids.value;
  getHairFixTeacher(_id).then((response) => {
    loading.value = false;
    form.value = response.data;
    if (response.data.tags != null) {
      form.value.tags = response.data.tags.split(",");
    }

    open.value = true;
    title.value = "修改补发师信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["hairFixTeacherRef"].validate((valid) => {
    if (form.value.tags != null) {
      form.value.tags = form.value.tags.toString();
    }

    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateHairFixTeacher(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      } else {
        // console.log(form.value);
        addHairFixTeacher(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除补发师信息编号为"' + _ids + '"的数据项？')
    .then(function () {
      loading.value = true;
      return delHairFixTeacher(_ids);
    })
    .then(() => {
      loading.value = true;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {})
    .finally(() => {
      loading.value = false;
    });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "business/hairFixTeacher/export",
    {
      ...queryParams.value,
    },
    `hairFixTeacher_${new Date().getTime()}.xlsx`
  );
}

/** 请求博卡同步接口 */
function updateTeacher() {
  syncLoading.value = true;
  updateBokaTeacher()
    .then((response) => {
      proxy.$modal.msgSuccess("同步成功");
      syncLoading.value = false;
      getList();
    })
    .finally(() => {
      syncLoading.value = false
      buttonLoading.value = false;
    });
}
</script>
